package com.asiainfo.kbisheet.util.yunnan.aaaacashbox

import groovy.util.logging.Slf4j
import groovy.xml.MarkupBuilder

import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.AutoAuthQueryParam
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.AutoAuthQueryResult
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.CreateAppRequestQueryParam
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.CreateAppRequestQueryResult
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.CreateResRequestQueryParam
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.CreateResRequestQueryResult
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.LocalAuthQueryParam
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.LocalAuthQueryResult
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.QueryAppOperJKStatusQueryParam
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.QueryJKStatusByIDQueryParam
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.QueryJKStatusByIDQueryResult
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.QueryResCmdJKStatusQueryParam
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.QueryResCmdJKStatusQueryResult
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.QueryAppOperJKStatusQueryResult
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.ReSendJKPassQueryParam
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.ReSendJKPassQueryResult
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.RemoteAuthQueryParam
import com.asiainfo.kbisheet.util.yunnan.aaaacashbox.vo.RemoteAuthQueryResult
import com.ultrapower.bussinesssupportservice.BussinessSupportService
import com.ultrapower.bussinesssupportservice.BussinessSupportServicePortType

/**
 * 基于对象传递的4A金库服务
 * @author kyunra
 *
 */
@Slf4j
class CashboxService {
	private BussinessSupportServicePortType bussinessSupportServicePortType

	CashboxService(){
		BussinessSupportService bussinessSupportService = new BussinessSupportService(new URL("http://10.168.51.217:8010/JKService/webservices/BussinessSupportService?WSDL"))
		bussinessSupportServicePortType = bussinessSupportService.getBussinessSupportServiceHttpPort()
	}

	public AutoAuthQueryResult autoAuth(AutoAuthQueryParam param){

		def writer = new StringWriter()
		def xml = new MarkupBuilder( writer )

		xml.queryParam {
			xml.serviceUserName(param.serviceUserName)
			xml.servicePwd(param.servicePwd)
			xml.requestID(param.requestID)
			xml.workOrderType(param.workOrderType)
			xml.workOrderID(param.workOrderID)
			xml.userIP(param.userIP)
		}
		log.info "queryResCmdJKStatus请求字符串：${writer.toString()}"

		String out0 = bussinessSupportServicePortType.autoAuth(writer.toString())

		log.info "queryResCmdJKStatus结果字符串：${out0}"

		def queryResult = new XmlSlurper().parseText(out0)
		AutoAuthQueryResult reuslt = new AutoAuthQueryResult(resultCode:queryResult.resultCode, authResult:queryResult.authResult, failReason:queryResult.failReason)
		reuslt
	}

	public CreateAppRequestQueryResult createAppRequest(CreateAppRequestQueryParam param){

		def writer = new StringWriter()
		def xml = new MarkupBuilder( writer )

		xml.queryParam {
			xml.serviceUserName(param.serviceUserName)
			xml.servicePwd(param.servicePwd)
			xml.rootTicket(param.rootTicket)
			xml.appAccount(param.appAccount)
			xml.resNum(param.resNum)
			xml.functionCode(param.functionCode)
			xml.operationCode(param.operationCode)
			xml.authMode(param.authMode)
			xml.applyReason(param.applyReason)
			xml.userTimes(param.userTimes)
			xml.duration(param.duration)
			xml.userIP(param.userIP)
		}
		log.info "queryResCmdJKStatus请求字符串：${writer.toString()}"

		String out0 = bussinessSupportServicePortType.createAppRequest(writer.toString())

		log.info "queryResCmdJKStatus结果字符串：${out0}"

		def queryResult = new XmlSlurper().parseText(out0)
		CreateAppRequestQueryResult reuslt = new CreateAppRequestQueryResult(resultCode:queryResult.resultCode,applyStatus:queryResult.applyStatus,requestID:queryResult.requestID,authModes:[], approvers:[])
		queryResult.authMode.each {authMode ->
			reuslt.authModes.add(authMode)
		}

		queryResult.approver.each {
			reuslt.approvers.add(it)
		}
		reuslt
	}

	public CreateResRequestQueryResult createResRequest(CreateResRequestQueryParam param){

		def writer = new StringWriter()
		def xml = new MarkupBuilder( writer )

		xml.queryParam {
			xml.serviceUserName(param.serviceUserName)
			xml.servicePwd(param.servicePwd)
			xml.userID(param.userID)
			xml.resAccount(param.resAccount)
			xml.resID(param.resID)
			xml.resIP(param.resIP)
			xml.port(param.port)
			xml.sessionID(param.sessionID)
			xml.sensitiveCMD(param.sensitiveCMD)
			xml.applyReason(param.applyReason)
			xml.authMode(param.authMode)
			xml.userTimes(param.userTimes)
			xml.duration(param.duration)
			xml.userIP(param.userIP)
			xml.policyID(param.policyID)
		}
		log.info "queryResCmdJKStatus请求字符串：${writer.toString()}"

		String out0 = bussinessSupportServicePortType.createResRequest(writer.toString())

		log.info "queryResCmdJKStatus结果字符串：${out0}"

		def queryResult = new XmlSlurper().parseText(out0)
		CreateResRequestQueryResult reuslt = new CreateResRequestQueryResult(resultCode:queryResult.resultCode,applyStatus:queryResult.applyStatus,requestID:queryResult.requestID,authModes:[], approvers:[])
		queryResult.authMode.each {authMode ->
			reuslt.authModes.add(authMode)
		}

		queryResult.approver.each {
			reuslt.approvers.add(it)
		}
		reuslt
	}


	public LocalAuthQueryResult localAuth(LocalAuthQueryParam param){

		def writer = new StringWriter()
		def xml = new MarkupBuilder( writer )

		xml.queryParam {
			xml.serviceUserName(param.serviceUserName)
			xml.servicePwd(param.servicePwd)
			xml.requestID(param.requestID)
			xml.userID(param.userID)
			xml.passCode(param.passCode)
			xml.dynamicCode(param.dynamicCode)
			xml.pinCode(param.pinCode)
			xml.userIP(param.userIP)
		}
		log.info "queryResCmdJKStatus请求字符串：${writer.toString()}"

		String out0 = bussinessSupportServicePortType.localAuth (writer.toString())

		log.info "queryResCmdJKStatus结果字符串：${out0}"

		def queryResult = new XmlSlurper().parseText(out0)
		LocalAuthQueryResult reuslt = new LocalAuthQueryResult(resultCode:queryResult.resultCode, authResult:queryResult.authResult, failReason:queryResult.failReason)
		reuslt
	}


	public QueryAppOperJKStatusQueryResult queryAppOperJKStatus(QueryAppOperJKStatusQueryParam param){

		def writer = new StringWriter()
		def xml = new MarkupBuilder( writer )

		xml.queryParam {
			xml.serviceUserName(param.serviceUserName)
			xml.servicePwd(param.servicePwd)
			xml.rootTicket(param.rootTicket)
			xml.resNum(param.resNum)
			xml.appAccount(param.appAccount)
			xml.functionCode(param.functionCode)
			xml.operationCode(param.operationCode)
			xml.userIP(param.userIP)
		}
		log.info "queryResCmdJKStatus请求字符串：${writer.toString()}"

		String out0 = bussinessSupportServicePortType.queryAppOperJKStatus(writer.toString())

		log.info "queryResCmdJKStatus结果字符串：${out0}"

		def queryResult = new XmlSlurper().parseText(out0)
		QueryAppOperJKStatusQueryResult reuslt = new QueryAppOperJKStatusQueryResult(resultCode:queryResult.resultCode, next:queryResult.next, applyStatus:queryResult.applyStatus, requestID:queryResult.requestID, authModes:[], approvers:[])
		queryResult.authMode.each {authMode ->
			reuslt.authModes.add(authMode)
		}

		queryResult.approver.each {
			reuslt.approvers.add(it)
		}
		reuslt
	}

	public QueryJKStatusByIDQueryResult queryJKStatusByID(QueryJKStatusByIDQueryParam param){
		def writer = new StringWriter()
		def xml = new MarkupBuilder( writer )

		xml.queryParam {
			xml.serviceUserName(param.serviceUserName)
			xml.servicePwd(param.servicePwd)
			xml.requestID(param.requestID)
			xml.userIP(param.userIP)
		}
		log.info "queryResCmdJKStatus请求字符串：${writer.toString()}"

		String out0 = bussinessSupportServicePortType.queryJKStatusByID(writer.toString())

		log.info "queryResCmdJKStatus结果字符串：${out0}"

		def queryResult = new XmlSlurper().parseText(out0)
		QueryJKStatusByIDQueryResult reuslt = new QueryJKStatusByIDQueryResult(resultCode:queryResult.resultCode, applyStatus:queryResult.applyStatus, requestID:queryResult.requestID)
		reuslt
	}

	public QueryResCmdJKStatusQueryResult queryResCmdJKStatus(QueryResCmdJKStatusQueryParam param){
		def writer = new StringWriter()
		def xml = new MarkupBuilder( writer )

		xml.queryParam {
			xml.serviceUserName(param.serviceUserName)
			xml.servicePwd(param.servicePwd)
			xml.userID(param.userID)
			xml.resAccount(param.resAccount)
			xml.resID(param.resID)
			xml.resIP(param.resIP)
			xml.port(param.port)
			xml.sessionID(param.sessionID)
			xml.sensitiveCMD(param.sensitiveCMD)
			xml.userIP(param.userIP)
			xml.policyID(param.policyID)
		}
		log.info "queryResCmdJKStatus请求字符串：${writer.toString()}"

		String out0 = bussinessSupportServicePortType.queryResCmdJKStatus(writer.toString())

		log.info "queryResCmdJKStatus结果字符串：${out0}"

		def queryResult = new XmlSlurper().parseText(out0)
		QueryResCmdJKStatusQueryResult reuslt = new QueryResCmdJKStatusQueryResult(resultCode:queryResult.resultCode, next:queryResult.next, applyStatus:queryResult.applyStatus, requestID:queryResult.requestID,authModes:[], approvers:[])
		queryResult.authMode.each {authMode ->
			reuslt.authModes.add(authMode)
		}

		queryResult.approver.each {
			reuslt.approvers.add(it)
		}
		reuslt
	}

	public RemoteAuthQueryResult remoteAuth(RemoteAuthQueryParam param){
		def writer = new StringWriter()
		def xml = new MarkupBuilder( writer )

		xml.queryParam {
			xml.serviceUserName(param.serviceUserName)
			xml.servicePwd(param.servicePwd)
			xml.requestID(param.requestID)
			xml.userIP(param.userIP)
		}
		log.info "queryResCmdJKStatus请求字符串：${writer.toString()}"

		String out0 = bussinessSupportServicePortType.remoteAuth(writer.toString())

		log.info "queryResCmdJKStatus结果字符串：${out0}"

		def queryResult = new XmlSlurper().parseText(out0)
		RemoteAuthQueryResult reuslt = new RemoteAuthQueryResult(resultCode:queryResult.resultCode, authResult:queryResult.authResult, failReason:queryResult.failReason)
		reuslt
	}

	public ReSendJKPassQueryResult reSendJKPass(ReSendJKPassQueryParam param){

		def writer = new StringWriter()
		def xml = new MarkupBuilder( writer )

		xml.queryParam {
			xml.serviceUserName(param.serviceUserName)
			xml.servicePwd(param.servicePwd)
			xml.requestID(param.requestID)
			xml.userIP(param.userIP)
		}
		log.info "queryResCmdJKStatus请求字符串：${writer.toString()}"

		String out0 = bussinessSupportServicePortType.reSendJKPass(writer.toString())

		log.info "queryResCmdJKStatus结果字符串：${out0}"

		def queryResult = new XmlSlurper().parseText(out0)
		ReSendJKPassQueryResult reuslt = new ReSendJKPassQueryResult(resultCode:queryResult.resultCode, authResult:queryResult.authResult, failReason:queryResult.failReason)
		reuslt
	}
}
